Skip to content

Remove 3-argument {_,}evaluate!!; clean up submodel code #960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 3, 2025

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Jun 25, 2025

This PR:

  • removes the one last usage of evaluate!!(model, varinfo, context) in the DynamicPPL codebase
  • removes _evaluate!!(model, varinfo, context) as this method was only being used for submodels

In return, it introduces:

  • _evaluate!!(submodel, varinfo, context, left). Yes, yes, I basically moved the implementation from one place to the other; but the whole point is that this was special-case behaviour that was only needed for submodels, so this at least makes it clearer.

It also removes a lot of wrapper code for submodels. For example, we don't have this ReturnedModelWrapper and Sampleable thing any more, instead, there's just Submodel which wraps a model.

I personally don't see the need for more than one layer of indirection for submodels (in terms of the data structure, the only difference between a submodel and a model is that the submodel carries information about whether it should be auto-prefixed). I had mucked around with this before in #815 and I didn't find any real problems with removing the wrappers there (in fact in that PR I went one step further and removed all wrappers, but that would have made it impossible to opt-out of prefixing and that's Bad(TM)).

I didn't add any new tests, but test/submodel.jl already contains quite extensive tests to make sure that everything behaves and it passes these tests so I'm quite happy.

#959 needs to be merged first. Done.

Follow-up from #952

Closes #720 (for good)

@penelopeysm penelopeysm changed the title Remove 3-argument _evaluate!!; clean up submodel code Remove 3-argument {_,}evaluate!!; clean up submodel code Jun 25, 2025
@penelopeysm penelopeysm force-pushed the py/no-context-evaluate-submodel branch from 3dc22f4 to 4bb2526 Compare June 25, 2025 17:01
@penelopeysm penelopeysm changed the base branch from breaking to py/move-submodel June 25, 2025 17:01
Copy link
Contributor

github-actions bot commented Jun 25, 2025

Benchmark Report for Commit aaf0bc6

Computer Information

Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 7763 64-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

Benchmark Results

|                 Model | Dimension |  AD Backend |      VarInfo Type | Linked | Eval Time / Ref Time | AD Time / Eval Time |
|-----------------------|-----------|-------------|-------------------|--------|----------------------|---------------------|
| Simple assume observe |         1 | forwarddiff |             typed |  false |                  8.7 |                 1.6 |
|           Smorgasbord |       201 | forwarddiff |             typed |  false |                670.9 |                42.5 |
|           Smorgasbord |       201 | forwarddiff | simple_namedtuple |   true |                429.6 |                50.9 |
|           Smorgasbord |       201 | forwarddiff |           untyped |   true |               1008.0 |                35.3 |
|           Smorgasbord |       201 | forwarddiff |       simple_dict |   true |               6377.6 |                25.0 |
|           Smorgasbord |       201 | reversediff |             typed |   true |               1495.3 |                27.5 |
|           Smorgasbord |       201 |    mooncake |             typed |   true |               1025.4 |                 4.3 |
|    Loop univariate 1k |      1000 |    mooncake |             typed |   true |               5851.1 |                 4.0 |
|       Multivariate 1k |      1000 |    mooncake |             typed |   true |               1000.1 |                 9.1 |
|   Loop univariate 10k |     10000 |    mooncake |             typed |   true |              65756.9 |                 3.6 |
|      Multivariate 10k |     10000 |    mooncake |             typed |   true |               8742.1 |                 9.9 |
|               Dynamic |        10 |    mooncake |             typed |   true |                139.7 |                11.9 |
|              Submodel |         1 |    mooncake |             typed |   true |                 13.7 |                 6.0 |
|                   LDA |        12 | reversediff |             typed |   true |               1697.6 |                 2.8 |

@penelopeysm penelopeysm force-pushed the py/no-context-evaluate-submodel branch from 4bb2526 to 55f838f Compare June 25, 2025 17:02
Copy link

codecov bot commented Jun 25, 2025

Codecov Report

Attention: Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.

Project coverage is 82.77%. Comparing base (7f20709) to head (aaf0bc6).
Report is 1 commits behind head on breaking.

Files with missing lines Patch % Lines
src/context_implementations.jl 83.33% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           breaking     #960      +/-   ##
============================================
- Coverage     82.85%   82.77%   -0.09%     
============================================
  Files            38       38              
  Lines          4031     4011      -20     
============================================
- Hits           3340     3320      -20     
  Misses          691      691              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@penelopeysm penelopeysm force-pushed the py/no-context-evaluate-submodel branch 2 times, most recently from dcd5c9d to 31b0caa Compare June 25, 2025 20:41
Copy link
Contributor

DynamicPPL.jl documentation for PR #960 is available at:
https://TuringLang.github.io/DynamicPPL.jl/previews/PR960/

Base automatically changed from py/move-submodel to breaking June 26, 2025 00:57
@penelopeysm penelopeysm force-pushed the py/no-context-evaluate-submodel branch from 31b0caa to 039a523 Compare June 26, 2025 01:02
@penelopeysm penelopeysm marked this pull request as ready for review June 26, 2025 13:35
@penelopeysm penelopeysm requested a review from mhauru June 26, 2025 13:35
@penelopeysm penelopeysm force-pushed the py/no-context-evaluate-submodel branch from 039a523 to bfa48b3 Compare June 26, 2025 14:47
Copy link
Member

@mhauru mhauru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me, just had a few questions to check/inform myself.

Comment on lines -881 to -891
function AbstractPPL.evaluate!!(
model::Model, varinfo::AbstractVarInfo, context::AbstractContext
)
Base.depwarn(
"The `context` argument to evaluate!!(model, varinfo, context) is deprecated.",
:dynamicppl_evaluate_context,
)
new_ctx = combine_model_and_external_contexts(model.context, context)
model = contextualize(model, new_ctx)
return evaluate!!(model, varinfo)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't this deprecation added just a few weeks ago? Or am I mixing this with something else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above; but I don't know, I might be open to leaving a helpful error message in (i.e., maybe it shouldn't continue behaving as it does, but it should throw an error message that guides people towards the right answer)? I do know that upstream packages use evaluate!! even though it is internal, pretty sure I've seen it in Pigeons.jl for example, and Turing itself obviously uses evaluate!! quite a lot.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A manually crafted, more informative error message sounds good to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an error hint in src/DynamicPPL.jl, so now this happens:

julia> using DynamicPPL, Distributions

julia> @model f() = x ~ Normal()
f (generic function with 2 methods)

julia> m = f(); v = VarInfo(m); c = DefaultContext()
DefaultContext()

julia> DynamicPPL.evaluate!!(m, v, c)
ERROR: MethodError: no method matching evaluate!!(::Model{…}, ::VarInfo{…}, ::DefaultContext)
The function `evaluate!!` exists, but no method is defined for this combination of argument types.

The method `evaluate!!(model, varinfo, new_ctx)` has been removed. Instead, you should store the `new_ctx` in the `model.context` field using `new_model = contextualize(model, new_ctx)`, and then call `evaluate!!(new_model, varinfo)` on the new model. (Note that, if the model already contained a non-default context, you will need to wrap the existing context.)

Closest candidates are:
  evaluate!!(::Model, ::AbstractVarInfo)
   @ DynamicPPL ~/ppl/dppl/src/model.jl:868

Stacktrace:
 [1] top-level scope
   @ REPL[10]:1
Some type information was truncated. Use `show(err)` to see complete types.

@penelopeysm penelopeysm requested a review from mhauru July 3, 2025 14:29
Copy link
Member

@mhauru mhauru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @penelopeysm! I didn't know about adding error hints, that's neat.

@penelopeysm penelopeysm merged commit f20e86c into breaking Jul 3, 2025
20 of 21 checks passed
@penelopeysm penelopeysm deleted the py/no-context-evaluate-submodel branch July 3, 2025 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants